#!/bin/bash

# This is a part of the external applet Calendar for Cairo-Dock
#
# Copyright : (C) 2009 by Royohboy & Matttbe
#                         Thanks to Nochka85 for his demo
# E-mail : werbungfuerroy@googlemail.com and matttbe@gmail.com
#
#
# This program is free software; you can redistribute it and/or
# modify it under the term -fs of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/licenses.html#GPL

## Rev : 21/01/2010

DBUS_NAME="org.cairodock.CairoDock"
DBUS_PATH="/org/cairodock/CairoDock"
DBUS_INTERFACE="org.cairodock.CairoDock"
COMMAND=$0
SCRIPT_NAME=`basename $COMMAND`
APP_NAME="`echo $SCRIPT_NAME | cut -f1 -d '.' `"
ACTION=$1
DROP_DATA=$2
MENU_SELECT=$2
SCROLL_UP=$2
CONF_FILE="/home/$USER/.config/cairo-dock/current_theme/plug-ins/$APP_NAME/$APP_NAME.conf"

# My
LOCK_PROC=/tmp/cairo-dock-pa-plugin-proc-lock
LOCK_SOCKET=/tmp/cairo-dock-pa-plugin-pipe-lock
CMD_SOCKET=/tmp/cairo-dock-pa-plugin-pipe

#############################################################################################################
get_conf_param() {
  LIGNE=`cat $CONF_FILE | grep "$1"`
  PARAM="`echo $LIGNE | cut -f2 -d '=' `"
}

#############################################################################################################
get_ALL_conf_params() {

  get_conf_param "cdpavoctrl_command"
  cdpavoctrl_command="$PARAM"

  get_conf_param "import_command"
  import_command="$PARAM"

  #get_conf_param "reload_message"
  #reload_message="$PARAM"

  get_conf_param "time_dialog_cal_today"
  time_dialog_cal="$PARAM"

  get_conf_param "time_dialog_ev"
  time_dialog_ev="$PARAM"

  get_conf_param "time_dialog_cal_next"
  time_dialog_cal_next="$PARAM"

}

#############################################################################################################
mydbus(){
  dbus-send --session --dest=$DBUS_NAME $DBUS_PATH/$APP_NAME $DBUS_INTERFACE.$@
}


#############################################################################################################
action_on_click() {
  get_conf_param "time_dialog_cal_today"
  time_dialog_cal_today="$PARAM"

  dbus-send --session --dest=$DBUS_NAME $DBUS_PATH/$APP_NAME $DBUS_INTERFACE.applet.ShowDialog string:"Click" int32:$time_dialog_cal_today

  exit
}

#############################################################################################################
action_on_middle_click() {

  echo "$APP_NAME applet -> Script Name : $SCRIPT_NAME -> action_on_middle_click"

#   get_conf_param "cdpavoctrl_command"
#   cdpavoctrl_command="$PARAM"
# 
#   $cdpavoctrl_command

  exit
}


#############################################################################################################
action_on_scroll_icon() {

      [ $SCROLL_UP -eq "0" ] && scroll=scroll_up || scroll=scroll_down

      if flock -w 0 -x "${LOCK_PROC}" -c "true" ; then
#         echo START
        ./handler.sh &>>/dev/null &
#         ./handler.sh &>>/tmp/cdpavoctrl.log &
      else
        flock -w 0 -x "${LOCK_SOCKET}" -c "echo $scroll > ${CMD_SOCKET}" &>/dev/null
      fi
  exit
}

#############################################################################################################
action_on_drop_data() {
  echo "$APP_NAME applet -> Script Name : $SCRIPT_NAME -> Drop Data"
  
  get_conf_param "import_command"
  import_command="$PARAM"

  dbus-send --session --dest=$DBUS_NAME $DBUS_PATH/$APP_NAME $DBUS_INTERFACE.applet.ShowDialog string:"action_on_drop_data" int32:4
  $import_command $DROP_DATA

  exit
}

#############################################################################################################
action_on_init() {
  echo "$APP_NAME applet -> init"

#   ./icon.sh
  get_ALL_conf_params

  exit
}

#############################################################################################################
action_on_stop() {
  echo "$APP_NAME applet -> Script Name : $SCRIPT_NAME -> Stop"
  
#   killall update_calendar.sh
}

#############################################################################################################
action_on_reload() {
  get_ALL_conf_params
  echo "$APP_NAME applet -> Script Name : $SCRIPT_NAME -> Reload"
  exit
}


#############################################################################################################
# START ### DO NOT CHANGE THIS SECTION
#############################################################################################################

if [ "`echo $ACTION |grep 'register_the_applet'`" != "" ]; then
	register_the_applet
elif [ "`echo $ACTION |grep 'action_on_click'`" != "" ]; then
	action_on_click
elif [ "`echo $ACTION |grep 'action_on_middle_click'`" != "" ]; then
	action_on_middle_click
elif [ "`echo $ACTION |grep 'action_on_scroll_icon'`" != "" ]; then
	action_on_scroll_icon
elif [ "`echo $ACTION |grep 'action_on_drop_data'`" != "" ]; then
	action_on_drop_data
elif [ "`echo $ACTION |grep 'action_on_init'`" != "" ]; then
	action_on_init
elif [ "`echo $ACTION |grep 'action_on_stop'`" != "" ]; then
	action_on_stop
elif [ "`echo $ACTION |grep 'action_on_reload'`" != "" ]; then
	action_on_reload
elif [ "`echo $ACTION |grep 'action_on_build_menu'`" != "" ]; then
	action_on_build_menu
elif [ "`echo $ACTION |grep 'action_on_menu_select'`" != "" ]; then
	action_on_menu_select
fi

exit

